package com.shiguiwu.springmybatis.javabase.thread;

import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @description: future原理探究
 * @author: stone
 * @date: Created by 2021/12/5 22:34
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.thread
 */
public class FutureTests {


    public static void main(String[] args) throws Exception {
        ExecutorService service = Executors.newFixedThreadPool(10);
        try {
            Future<Integer> future = service.submit(new MyCallable());

            TimeUnit.SECONDS.sleep(3);
            System.out.println(future.get(5,TimeUnit.SECONDS));
        } finally {
            service.shutdown();
        }

    }

    public static class MyCallable  implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            TimeUnit.SECONDS.sleep(1);
            return RandomUtil.randomInt(1, 100);
        }
    }
}
